package com.jxc.jxc01.yk.web;

import com.alibaba.fastjson.JSONObject;
import com.jxc.jxc01.yk.entity.OutWarehouse;
import com.jxc.jxc01.yk.entity.PageHelper;
import com.jxc.jxc01.yk.entity.RespResult;
import com.jxc.jxc01.yk.service.IOutWarehouseService;
import com.jxc.jxc01.yk.service.OutWarehouseService;
import com.jxc.jxc01.yk.util.UUIDCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("*.out")
public class OutWarehouseServlet extends HttpServlet {
    IOutWarehouseService outWarehouseService = new OutWarehouseService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //获取请求地址
        String uri = request.getRequestURI();
        uri = uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf("."));

        if (uri.equals("ListOutWarehouse")) {
            System.out.println("进入到=》ListOutWarehouse");
            findOutWarehouseList(request, response);
        } else if (uri.equals("deleteOutWarehouse")) {
            System.out.println("进入到=》deleteOutWarehouse");
            deleteOutWarehouse(request, response);
        } else if (uri.equals("updateOutWarehouse")) {
            System.out.println("进入到=》updateOutWarehouse");
            updateOutWarehouse(request, response);
        } else if (uri.equals("addOutWarehouse")) {
            System.out.println("进入到=》addOutWarehouse");
            addOutWarehouse(request, response);
        }else if (uri.equals("findOutWarehouseNumber")) {
            System.out.println("进入到=》findOutWarehouseNumber");
            findOutWarehouseNumber(request, response);
        }

    }

    /**
     * 添加出库信息
     */
    private void addOutWarehouse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<OutWarehouse> outWarehouses = new ArrayList<>();
        RespResult result = null;
        OutWarehouse outWarehouse = new OutWarehouse();
        //获取要增加的出库信息
        //生成一个uuid出库单号
        int outwarehouse_num = new UUIDCode().getUuid();
        outWarehouse.setOutwarehouse_num(outwarehouse_num);
        outWarehouse.setWarehouse_num(request.getParameter("warehouse_num"));
        outWarehouse.setOut_commodity_name(request.getParameter("out_commodity_name"));
        outWarehouse.setSpecification(request.getParameter("specification"));
        String out_warehouse_countStr = request.getParameter("outwarehouse_count");
        if (out_warehouse_countStr.equals(null) || out_warehouse_countStr.equals("")){
            out_warehouse_countStr = "0";
        }
        outWarehouse.setOutwarehouse_count(Integer.parseInt(out_warehouse_countStr));
        String outbound_unit_priceStr = request.getParameter("outbound_unit_price");
        if (outbound_unit_priceStr.equals(null) || outbound_unit_priceStr.equals("")){
            outbound_unit_priceStr = "0";
        }
        outWarehouse.setOutbound_unit_price(Double.parseDouble(outbound_unit_priceStr));
        outWarehouse.setTotalprice(outWarehouse.getOutbound_unit_price()*outWarehouse.getOutwarehouse_count());
        //outWarehouse.setOutwarehouse_count(Integer.parseInt(request.getParameter("out_warehouse_count")));
        //outWarehouse.setOutbound_unit_price(Double.parseDouble(request.getParameter("outbound_unit_price")));
        //outWarehouse.setTotalprice(Double.parseDouble(request.getParameter("totalprice")));
        outWarehouse.setWarehouse_person(request.getParameter("warehouse_person"));
        outWarehouse.setWhether_outbound(request.getParameter("whether_outbound"));
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowtime = sdf.format(date);
        outWarehouse.setCreate_time(nowtime);
        outWarehouse.setOutbound_time(nowtime);
        outWarehouses.add(outWarehouse);
        boolean addOutWarehouse = outWarehouseService.addOutWarehouse(outWarehouses);
        response.sendRedirect("chukuqingdan.html");
    }

    /**
     * 修改出库信息
     */
    private void updateOutWarehouse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OutWarehouse outWarehouse = new OutWarehouse();
        //获取要修改的信息
        outWarehouse.setId(Integer.parseInt(request.getParameter("id")));
        outWarehouse.setWarehouse_num(request.getParameter("warehouse_num"));
        outWarehouse.setOut_commodity_name(request.getParameter("out_commodity_name"));
        outWarehouse.setSpecification(request.getParameter("specification"));
        String out_warehouse_countStr = request.getParameter("outwarehouse_count");
        if (out_warehouse_countStr.equals(null) || out_warehouse_countStr.equals("")){
            out_warehouse_countStr = "0";
        }
        outWarehouse.setOutwarehouse_count(Integer.parseInt(out_warehouse_countStr));
        String outbound_unit_priceStr = request.getParameter("outbound_unit_price");
        if (outbound_unit_priceStr.equals(null) || outbound_unit_priceStr.equals("")){
            outbound_unit_priceStr = "0";
        }
        outWarehouse.setOutbound_unit_price(Double.parseDouble(outbound_unit_priceStr));
        outWarehouse.setTotalprice(outWarehouse.getOutbound_unit_price()*outWarehouse.getOutwarehouse_count());
        outWarehouse.setWarehouse_person(request.getParameter("warehouse_person"));
        outWarehouse.setWhether_outbound(request.getParameter("whether_outbound"));
        //修改新的出库时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String outbound_time = sdf.format(date);
        outWarehouse.setOutbound_time(outbound_time);
        System.out.println(outWarehouse.toString());
        boolean updateOutWarehouse = outWarehouseService.updateOutWarehouse(outWarehouse);
        if (!updateOutWarehouse) {
            //修改失败
            return;
        }
        response.sendRedirect("chukuqingdan.html");
    }

    /**
     * 根据出库单号查询出库信息
     */
    private void findOutWarehouseNumber(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        List<OutWarehouse> outWarehouses = new ArrayList<>();
        //获取要查找的出库单号
        String outwarehouse_numstr = request.getParameter("outwarehouse_num");
        if (outwarehouse_numstr == null || outwarehouse_numstr==""){
            outwarehouse_numstr = "0";
        }
        int outwarehouse_num = Integer.parseInt(outwarehouse_numstr);
        outWarehouses = outWarehouseService.findOutWarehouseNumber(outwarehouse_num);
        RespResult result = new RespResult(0, "ok", outWarehouses.size(), outWarehouses);
        HttpSession session = request.getSession();
        session.setAttribute("findOutWarehouseNumber", result);
        response.sendRedirect("chukuqingdan.html");
    }

    /**
     * 删除某一条出库记录
     */
    private void deleteOutWarehouse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<OutWarehouse> outWarehouses = new ArrayList<>();
        RespResult result = null;
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println("id="+id);
        //删除某一条出库记录
        boolean deleteOutWarehouse = outWarehouseService.deleteOutWarehouse(id);
        response.sendRedirect("chukuqingdan.html");
    }

    /**
     * 查询所有出库记录
     */
    private void findOutWarehouseList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //查询session
        HttpSession session = request.getSession();
        RespResult findOutWarehouseNumber = (RespResult) session.getAttribute("findOutWarehouseNumber");
        PageHelper<OutWarehouse> pageHelper = new PageHelper();
        if (findOutWarehouseNumber == null){
            List<OutWarehouse> outWarehouses = new ArrayList<>();
            //查询出库记录
            outWarehouses = outWarehouseService.findOutWarehouseList(pageHelper);
            RespResult result = new RespResult(0,"ok",pageHelper.getTotal(),outWarehouses);
            out.println(JSONObject.toJSONString(result));
        }else {
            out.println(JSONObject.toJSONString(findOutWarehouseNumber));
            session.removeAttribute("findOutWarehouseNumber");
        }

    }

}
